package com.artwork.controller;

import com.artwork.common.domain.AjaxResult;
import com.artwork.common.enums.JsonResultEnum;
import com.artwork.common.exception.JsonException;
import com.artwork.model.Goods;
import com.artwork.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;

/**
 * @Author JoJo
 * @Data 2021/9/9$ 14:46$
 * @Description
 * @Param $
 * @Return $
 */
@RestController
@RequestMapping("/admin/goods")
public class GoodsController {
    @Autowired
    GoodsService goodsService;

    /**
     * @desc 新增商品 来自平台或者代金券
     * @param goods
     * @return
     */
    @PostMapping("/add")
    public AjaxResult addGoods(Goods goods){
        goods.setCreateTime(System.currentTimeMillis());
        goods.setUpdateTime(System.currentTimeMillis());
        goodsService.addGoos(goods);
        return AjaxResult.success();
    }

    /**
     * @desc 修改商品
     * @param goods
     * @return
     * @throws JsonException
     */
    @PostMapping("/update")
    public AjaxResult updateGoods(Goods goods) throws JsonException {
        Goods goods1 = goodsService.getGoodsById(goods.getId());
        if (goods1 == null) {
            throw new JsonException(JsonResultEnum.TARGET_NULL);
        }
        goods.setUpdateTime(System.currentTimeMillis());
        goodsService.updateGoods(goods);
        return AjaxResult.success();
    }

    /**
     * @desc 上、下架
     * @param id
     * @return
     * @throws JsonException
     */
    @PostMapping("/status")
    public AjaxResult pushGoods(int id, int status) throws JsonException {
        Goods goods = goodsService.getGoodsById(id);
        if (goods == null) {
            throw new JsonException(JsonResultEnum.TARGET_NULL);
        }
        goodsService.pushGoods(id, status);
        return AjaxResult.success();
    }

    /**
     * @desc 删除货物
     * @param id
     * @return
     * @throws JsonException
     */
    @PostMapping("/delete")
    public AjaxResult deleteGoods(int id) throws JsonException {
        Goods goods = goodsService.getGoodsById(id);
        if (goods == null) {
            throw new JsonException(JsonResultEnum.TARGET_NULL);
        }
        goodsService.deleteGoods(id);
        return AjaxResult.success();
    }

}
